package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.S_in_authority;
import com.cn.jdia.tas.model.S_interest;
import com.cn.jdia.tas.model.T_attrecord;
import com.cn.jdia.tas.module.DailySheetModule;
import com.cn.jdia.tas.service.IAttrecordService;
import com.cn.jdia.tas.service.IInterestAuthority;
import com.cn.jdia.tas.service.IInterestService;
import com.cn.jdia.tas.utils.ListUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class InterestController {

	@Resource
	private IInterestAuthority authorityService = null;

	@Resource
	private IInterestService interestService = null;

	@Resource
	private IAttrecordService attrecordService = null;

	@RequestMapping("/interest")
	public String getInterestData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String id = pmap.get("id");
		String date = pmap.get("date");

		System.out.println(id + " , " + date);

		Map<String, Object> map = new HashMap<>();

		// ClassPathXmlApplicationContext w_context = new
		// ClassPathXmlApplicationContext("spring-mybatis.xml");
		// authorityService = (IInterestAuthority)
		// w_context.getBean("inAuthorityService");

		// authorityService=(IInterestAuthority)SpringContextUtils.getContext().getBean("inAuthorityService");

		S_in_authority authority = authorityService.checkByEmployeeId(id);

		if (authority == null) {
			System.out.println("has no authority");
			map.put("valid", "false");
			out.write(gson.toJson(map));
			out.flush();
			out.close();
			return null;
		}

		System.out.println(authority.getAuthority());
		map.put("valid", "true");

		// interestService = (IInterestService)
		// w_context.getBean("interestService");
		interestService = (IInterestService) SpringContextUtils.getContext().getBean("interestService");

		List<String> interestList = interestService.getInterestList(id);
		int size = interestList.size();
		map.put("size", size);
		map.put("inlist", interestList);

		// interest list is null
		if (size == 0) {
			out.write(gson.toJson(map));
			out.flush();
			out.close();
			return null;
		}

		// attrecordService = (IAttrecordService)
		// w_context.getBean("attrecordService");
		attrecordService = (IAttrecordService) SpringContextUtils.getContext().getBean("attrecordService");

		List<T_attrecord> attList = attrecordService.getInterestAttrecords(date, interestList);

		String name;
		String iid;
		String idate;
		String time;
		String type;
		String no;
		String machine;

		Map<String, DailySheetModule> dmap = new HashMap<>();

		for (T_attrecord acc : attList) {
			// System.out.println(acc.getChecktime() + " , " +
			// acc.getEmployee().getEmployeename());

			name = acc.getEmployee().getEmployeename();
			iid = acc.getEmployeeid();
			idate = acc.getCheckdate();
			time = acc.getChecktime();
			type = acc.getChecktype();
			no = acc.getEnrollno();
			machine = acc.getAttmachinename();

			String mkey = date + "" + name;
			if (!dmap.keySet().contains(mkey)) {
				DailySheetModule mo = new DailySheetModule();
				mo.setId(iid);
				mo.setName(name);
				mo.setDate(idate);
				mo.setBegTime(time);
				mo.setEndTime("");

				dmap.put(mkey, mo);

			} else {
				DailySheetModule mo = dmap.get(mkey);

				String begTime = mo.getBegTime();
				String endTime = mo.getEndTime();

				if ((endTime == "") || (endTime == null)) {
					mo.setEndTime(time);
				} else {
					if (endTime.compareTo(time) < 0) {
						mo.setEndTime(time);
					}
				}
				dmap.put(mkey, mo);
			}
		}

		map.put("acc", dmap);

		out.write(gson.toJson(map));
		out.flush();
		out.close();
		return null;
	}

	@RequestMapping("/updateinterest")
	public String updateInterestList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, List<String>> pmap = gson.fromJson(data, new TypeToken<Map<String, List<String>>>() {
		}.getType());

		List<String> pid = pmap.get("pid");
		List<String> orilist = pmap.get("orilist");
		List<String> finlist = pmap.get("finlist");

		String employeeid = pid.get(0);

		// System.out.println(employeeid);
		// System.out.println(orilist);
		// System.out.println(finlist);

		List<String> delete = ListUtils.listCompare(orilist, finlist);
		List<String> insert = ListUtils.listCompare(finlist, orilist);

		// System.out.println("delete:"+delete);
		// System.out.println("insert"+insert);

		// there is no record to modify
		if ((delete.size() == 0) && (insert.size() == 0)) {
			out.write("fuwa");
			out.flush();
			out.close();
			return null;
		}

		// ClassPathXmlApplicationContext w_context = new
		// ClassPathXmlApplicationContext("spring-mybatis.xml");
		// interestService = (IInterestService)
		// w_context.getBean("interestService");

		interestService = (IInterestService) SpringContextUtils.getContext().getBean("interestService");

		if (delete.size() != 0) {
			interestService.deleteInterestRecord(employeeid, delete);
		}

		if (insert.size() != 0) {
			for (String in : insert) {
				S_interest s_interest = new S_interest();
				s_interest.setSponsorid(employeeid);
				s_interest.setInterestid(in);
				int ret = interestService.insertInterestRecord(s_interest);
			}
		}

		out.write("kira");
		out.flush();
		out.close();
		return null;
	}

}
